home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-1.iso / Files / Internet / Misc / Uupc 3.1 sources.sit / uupc 3.1 sources Folder / Mac specific / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-21  |  4.4 KB  |  253 lines  |  [TEXT/KAHL]

  1. /*         lib.c
  2.  
  3. */
  4.  
  5. #ifdef THINK_C
  6. # include "unixlibproto.h"
  7. #endif THINK_C
  8.  
  9. #include <stdio.h>
  10. #include <ctype.h>
  11. #include <errno.h>
  12. #include "host.h"
  13. #ifdef     THINK_C
  14. # include <unix.h>
  15. # include <fcntl.h>
  16. #endif
  17.  
  18. #ifndef NULL
  19. #define NULL 0L
  20. #endif
  21.  
  22. #include "lib.proto.h"
  23.  
  24. MKDIR(char *path)
  25. {
  26.     char * cp = path;
  27.     char *colon, *slash, *sep, sepchar;
  28.     OSErr err;
  29.     int first = 1;
  30.  
  31.     if ( *cp == '\0' )
  32.         return( 0 );
  33.  
  34.     /* see if we need to make any intermediate directories */
  35.     while (cp && *cp) {
  36.         slash = index (cp, SEPCHAR);
  37.         colon = index (cp, DIRCHAR);
  38.         sep = slash;
  39.         if (colon && (!slash || colon < slash)) {
  40.             sep = colon;
  41.         }
  42.         if (sep) {
  43.             sepchar = *sep;
  44.             if (sepchar != DIRCHAR || !first /* || sep != path */) {
  45.                 *sep = '\0';
  46.                 err = mkdir( path );
  47.                 if (err != noErr && err != dupFNErr) {
  48.                     printmsg(0, "mkdir error %d on %s", err, path);
  49.                 }
  50.                 *sep = sepchar;
  51.             }
  52.             sep++;
  53.             first = 0;
  54.         }
  55.         cp = sep;
  56.     }
  57.  
  58.     /* make last dir */
  59.     err = mkdir(path);
  60.     if (err != noErr && err != dupFNErr) {
  61.         printmsg(0, "mkdir error %d on %s", err, path);
  62.     }
  63.     return( err );
  64.  
  65. }
  66.  
  67. CHDIR(char *path)
  68. {
  69.     char * cp = path;
  70.  
  71.     if ( *cp == '\0' )
  72.         return( 0 );
  73.  
  74.     MKDIR( path );
  75.  
  76.     /* change to last directory */
  77.     return( chdir( path ) );
  78.  
  79. }
  80.  
  81. int OPEN (char *name, int mode)
  82. {
  83.     char   nname[255];
  84.     int        results;
  85.     cnvMac(name, nname);
  86.     mapMacCaseness(nname);
  87.     results = open( nname, mode );
  88.     return results;
  89. }
  90.  
  91. FILE * FOPEN(char *name, char *mode, char ftype)
  92. {
  93.  
  94.     char * last;
  95.     FILE * results;
  96.     char   nname[255];
  97.     char   opmode[5];
  98.     int       len;
  99.  
  100.     /* are we opening for write or append */
  101.  
  102.     FILEMODE( ftype );
  103.     strcpy(opmode, mode);
  104.     if (ftype == 'b') {
  105.         len = strlen(opmode);
  106.         *(opmode+len) = ftype;
  107.         *(opmode+len+1) = '\0';
  108.     }
  109. #if 0
  110.     DebugStr("\pIn FOPEN");
  111. #endif
  112.     cnvMac(name, nname);
  113.     mapMacCaseness(nname);
  114.     results = fopen( nname, opmode );
  115.  
  116.     if ( results != (FILE *) NULL ) {        /* fixed [garym 3/21/90 */
  117.         /* success, if not reading, set file info */
  118.         if (*mode != 'r') {
  119.             FInfo    fInfo;
  120.             CtoPstr(nname);
  121.             if (GetFInfo((StringPtr)nname, 0, &fInfo) == noErr) {
  122.                 fInfo.fdCreator = 'MPS ';
  123.                 (void)SetFInfo((StringPtr)nname, 0, &fInfo);
  124.             }
  125.         }
  126.         return( results );
  127.     }
  128.     /* are we opening in sub-directory */
  129.     last = rindex( name, SEPCHAR );
  130.  
  131.     /* lets just verify that all sub-dir's exist */
  132.     if ( last != (char *) NULL ) {
  133.         *last = '\0';
  134.         MKDIR( name );
  135.         *last = '/';
  136.     }
  137.  
  138.     /* now try open again */
  139.     return( fopen( nname, opmode ));
  140.  
  141. }
  142.  
  143. int CREAT(char *name, int mode, char ftyp)
  144. {
  145.  
  146.     char *    last;
  147.     int     results;
  148.     char    nname[255];
  149.     FILE *fp;
  150.     
  151.     /* are we opening for write or append */
  152.     FILEMODE( ftyp );
  153.     cnvMac(name, nname);
  154.     mapMacCaseness(nname);
  155.     mode = O_CREAT|O_TRUNC|O_WRONLY;    /* ignore Unix perms */
  156.     if (ftyp == 'b') mode |= O_BINARY;
  157.     else mode |= O_TEXT;    
  158.     results = creat( nname, mode );
  159.  
  160.     if ( results != -1 )
  161.         return( results );    /* success */
  162.  
  163.     /* are we opening in sub-directory */
  164.     last = rindex( name, '/' );
  165.  
  166.     /* lets just verify that all sub-dir's exist */
  167.     if ( last != (char *) NULL ) {
  168.         *last = '\0';
  169.         MKDIR( name );
  170.         *last = '/';
  171.     }
  172.  
  173.     /* now try open again */
  174.     results = creat( nname, mode );
  175.     if ( results != -1 )
  176.         return( results );    /* success */
  177.         
  178.     /* maybe it already exists, see if we can open it */
  179.     fp = FOPEN( nname, "w", ftyp );
  180.     if ( fp != NULL ) {
  181.         fclose(fp);
  182.         results = 0;        /* it already exists, no problem */
  183.     } else {
  184.         results = -1;
  185.     }
  186.         
  187.     return( results );
  188.  
  189. }
  190.  
  191. int    UNLINK(char *path)
  192. {
  193.     char    mpath[255];
  194.     cnvMac(path, mpath);
  195.     mapMacCaseness(mpath);
  196.     return(unlink(mpath));
  197. }
  198.  
  199.  
  200. extern int debuglevel;
  201. extern int remote;
  202.  
  203. #define MASTER 1
  204.  
  205.  
  206. int getargs(char *line, char **flds)
  207. {
  208.     int i = 0;
  209.     char *s, quoteChar;
  210.     int quoteit, backslash;
  211.     
  212.     quoteChar = 0;
  213.  
  214.     while ( (*line != '\0') && (*line != '\n') )
  215.     {
  216.        if ( isspace(*line) )
  217.        {
  218.           line++;
  219.           continue;
  220.        }
  221.        if (*line == '"' || *line == '\'') {
  222.          quoteit = 1;
  223.          quoteChar = *line;
  224.          line++;
  225.        } else {
  226.          quoteit = 0;
  227.        }
  228.        backslash = 0;
  229.        *flds++ = line;
  230.        i++;
  231.        while (*line != '\0') {
  232.          if (!quoteit && !backslash && isspace(*line)) break;
  233.          if (backslash) {
  234.              backslash = 0;
  235.          } else {
  236.              if (*line == '\\') {
  237.                 backslash = 1;
  238.              } else if (*line == quoteChar) {
  239.                 quoteit = !quoteit;
  240.              }
  241.          }
  242.          line++;
  243.        }
  244.        if (quoteChar != 0 && *(line-1) == quoteChar) {
  245.                *(line-1) = '\0';
  246.        }
  247.        if (isspace(*line)) *line++ = '\0';
  248.     }
  249.     return(i);
  250. }
  251.  
  252.  
  253.